package com.kamistoat.meimeistore.coupon.service.impl;

import com.kamistoat.common.To.SecKillTo.SecKillProductTo;
import com.kamistoat.common.To.SecKillTo.SecKillSessionTo;
import com.kamistoat.common.utils.R;
import com.kamistoat.meimeistore.coupon.entity.SeckillSkuRelationEntity;
import com.kamistoat.meimeistore.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.Query;

import com.kamistoat.meimeistore.coupon.dao.SeckillSessionDao;
import com.kamistoat.meimeistore.coupon.entity.SeckillSessionEntity;
import com.kamistoat.meimeistore.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取最近三天的秒杀活动
     *
     * @return
     */
    @Override
    public List<SecKillSessionTo> get3DaySession() {
        LocalDate nowTime = LocalDate.now();  // 直接用库方法获取当前日期
        LocalDate startDay = nowTime.plusDays(1);// 在这个时间的基础上再加一天
        LocalDate endDay = nowTime.plusDays(2);// 在这个时间的基础上加2天
        // String startTime = startDay.toString() + " 00:00:00";  // 拼接上零点时刻
        String startTime = LocalDate.now().toString() + " 00:00:00";  // 开发期间设置为从过去一天开始。不然没法调试此刻的秒杀
        String endTime = endDay.toString() + " 23:59:59";  // 拼接上24点时刻

        // 可以直接使用wrapper.between()来进行条件查询。永远都是传递String类型数据，MySql会自动匹配格式
        List<SeckillSessionEntity> secKillSessionList = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));

        // 构建 SecKillSessionTo
        if (secKillSessionList != null && secKillSessionList.size() > 0) {
            // 如果活动不是空的，就把这些Entity类转换成To类，顺便把每场活动对应的商品也查出来
            List<SecKillSessionTo> sessionToList = secKillSessionList.stream().map(seckillSessionEntity -> {
                SecKillSessionTo secKillSessionTo = new SecKillSessionTo();
                // 拷贝Entity到To
                BeanUtils.copyProperties(seckillSessionEntity, secKillSessionTo);
                // 查询对应的商品
                List<SeckillSkuRelationEntity> sessionRelationList =
                        seckillSkuRelationService.list(
                                new QueryWrapper<SeckillSkuRelationEntity>().
                                        eq("promotion_session_id", seckillSessionEntity.getId()));
                // 把SeckillSkuRelationEntity类转变为SecKillProductTo类
                if (sessionRelationList == null || sessionRelationList.size() <= 0) {
                    secKillSessionTo.setProductToList(null);
                } else {
                    List<SecKillProductTo> productToList = sessionRelationList.stream().map(seckillSkuRelationEntity -> {
                        SecKillProductTo secKillProductTo = new SecKillProductTo();
                        BeanUtils.copyProperties(seckillSkuRelationEntity, secKillProductTo);
                        return secKillProductTo;
                    }).collect(Collectors.toList());
                    secKillSessionTo.setProductToList(productToList);
                }
                return secKillSessionTo;
            }).collect(Collectors.toList());
            return sessionToList;
        }
        return null;
    }

    /**
     * 检查secKillSession，不允许创建重复时间内的session
     *
     * @param seckillSession
     */
    @Override
    public R checkSeckillSession(SeckillSessionEntity seckillSession) {
        Date startTime = seckillSession.getStartTime();
        Date endTime = seckillSession.getEndTime();
        List<Long> clashSessionList = baseMapper.getClashSession(startTime, endTime);
        if(!clashSessionList.isEmpty() || clashSessionList.size() > 0){
            String msg = "";
            for (Long aLong : clashSessionList) {
                msg = msg + aLong.toString() + "/";
            }
            return R.error(1, "该秒杀时间与" + msg + "号秒杀活动冲突");
        }else{
            return R.ok();
        }
    }
}